<?php
/**
 * The SIDN EPP Contact Info Object
 *
 * This will hold the complete contact info SIDN can receive and give you
 *
 *
 *
 * @author Ralph de Boom <php5-sidnepp@mijndomein.nl>
 * @author Peter Keizer <php5-sidnepp@mijndomein.nl>
 * @license http://www.gnu.org/licenses/gpl-3.0.txt GNU Public License v3
 * @copyright http://www.mijndomein.nl/
 * @version 0.5
 * @link http://php5-sidnepp.svn.sourceforge.net/viewvc/php5-sidnepp/trunk/dev/docs/index.html
 * @see http://php5-sidnepp.svn.sourceforge.net/viewvc/php5-sidnepp/trunk/dev/docs/php5-sidnepp/SidnContactInfo.html
 * @package php5-sidnepp
 */

class SidnContactInfo
{
	private $postalInfo;
	private $voice;
	private $fax;
	private $email;
	private $legalForm;


	/**
	 *
	 * @param string $contactHandle
	 * @param SidnContactPostalInfo $postalInfo
	 * @param string $email
	 * @param string $password
	 * @param string $voice
	 * @param string $fax
	 * @return void
	 */
	public function __construct(SidnContactPostalInfo $postalInfo,
			$email, $legalForm, $password, $voice = null, $fax = null)
	{
		$this->addPostalInfo($postalInfo);

		$this->setEmail($email);
		$this->setLegalForm($legalForm);

		$this->setPassword($password);
		$this->setVoice($voice);
		$this->setFax($fax);

	}
	/**
	 * Add postal info to this contact
	 * @param SidnContactPostalInfo $postalInfo
	 * @return void
	 */
	public function addPostalInfo(SidnContactPostalInfo $postalInfo)
	{
		if (count($this->postalInfo) < 2)
		{
			$this->postalInfo[count($this->postalInfo)] = $postalInfo;
		}
		else
		{
			Throw new SidnEppException('You can\'t add more than 2 postalInfo\'s for one contact!');
		}
	}
	/**
	 * Gets the total number of postalinfo objects this contact holds
	 * @return int
	 */
	public function getPostalInfoLength()
	{
		return count($this->postalInfo);
	}
	/**
	 * Retrieve a postalInfo object by number
	 *
	 * @param int $line
	 * @return SidnContactPostalInfo
	 */
	public function getPostalInfo($line)
	{
		if ($this->postalInfo[$line])
		{
			return $this->postalInfo[$line];
		}
		else
		{
			return null;
		}
	}
	/**
	 * Sets the email address
	 * @param string $email
	 * @return void
	 */
	public function setEmail($email)
	{
		$this->email = $email;

	}
	/**
	 * Gets the email address
	 * @return string
	 */
	public function getEmail()
	{
		return $this->email;
	}
	/**
	 * Sets the password
	 *
	 * **NOTE** This is not used by SIDN at the moment, but they do require it to be given
	 * @param string $password
	 * @return void
	 */

	public function setPassword($password)
	{
		$this->password = $password;

	}
	/**
	 * Gets the password
	 * @return string
	 */
	public function getPassword()
	{
		return $this->password;
	}
	/**
	 * Sets the phone number
	 * @param int $voice
	 * @return void
	 */
	public function setVoice($voice)
	{

		$this->voice = $this->generatePhoneNumber($voice);

	}
	/**
	 * Gets the phone number
	 * @return string
	 */
	public function getVoice()
	{
		return $this->voice;
	}
	/**
	 * Sets the fax number
	 * @param int $fax
	 * @return void
	 */
	public function setFax($fax)
	{
		$this->fax = $this->generatePhoneNumber($fax);
	}
	/**
	 * Gets the fax number
	 * @return string
	 */
	public function getFax()
	{
		return $this->fax;
	}
	/**
	 * Sets the legal form
	 * @param string $legalForm
	 * @return void
	 */
	public function setLegalForm($legalForm)
	{
		$this->legalForm = $legalForm;
	}
	/**
	 * Gets the legal form
	 * @return string
	 */
	public function getLegalForm()
	{
		return $this->legalForm;
	}
	/**
	 * Formats the phone number according to SIDN formatting rules
	 * @param int $number
	 * @return string
	 */
	private function generatePhoneNumber($number)
	{
		//Format the phone number according to SIDN formatting rules.
		$number = preg_replace('/[^\d]/i', '', $number);
		if (!strlen($number))
		{
			return null;
		}
		if (substr($number, 0, 2) == '00')
		{
			$number = '+'.substr($number, 2);
		}
		else
		{
			$number = '+'.$number;
		}
		$number = substr($number, 0, 3).'.'.substr($number, 3);
		return $number;
	}
	/**
	 * Transforms the SidnContactInfo object into xml
	 * @param boolean $update: Set to true if you are using xml to update contact info.
	 *	                       This will pass the value to $postalInfo->toXml, to omit the name field
	 * @return string
	 */
	public function toXml($update = false)
	{
		$vars = array();
		$vars['postalInfo'] = '';
		//Let's generate the PostalInfo Xml bit first...
		for ($i = 0; $i < $this->getPostalInfoLength(); $i++)
		{
			$postalInfo = $this->getPostalInfo($i);
			$vars['postalInfo'] .= $postalInfo->toXml($update);
		}
		$vars['email']		= $this->getEmail();
		if (strlen($this->getVoice()))
		{
			$vars['voice']		= '<contact:voice><![CDATA['.$this->getVoice().']]></contact:voice>';
		}
		if (strlen($this->getFax()))
		{
			$vars['fax']		= '<contact:fax><![CDATA['.$this->getFax().']]></contact:fax>';
		}

		$vars['password']	= $this->getPassword();
		$vars['legalForm']	= $this->getLegalForm();
		//And finally generate the xml
		$contactInfoXml = SidnTools::getXml('contact_contactinfo.xml', $vars);
		return $contactInfoXml;

	}
}